package com.hq.talk;

import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hq.lib_comview.common.utils.LogUtils;
import com.hq.lib_comview.common.utils.StringUtils;
import com.hq.lib_comview.common.utils.SystemUtils;
import com.hq.talk.common.Session;
import com.hq.talk.util.ClearCacheUtil;
import com.hq.talk.view.base.BaseAty;
import com.hq.talk.view.base.BaseCommAty;
import com.hq.talk.view.ui.MainAty;

/**
 * 作者：Administrator on 2016/6/22 0022 11:07
 * 功能：欢迎页+广告页
 * 说明：展示欢迎页+广告页
 */
public class SplashAty extends BaseCommAty {

    /**
     * 跳过
     */
    private TextView mTxtjumpTo;

    /**
     * 启动页背景
     */
    private ImageView mImgBg;

    /**
     * 界面广告停留时间
     */
    private int recLen;

    /**
     * 倒计时类
     */
    private MyCount mc;

    //是否点击广告
    private boolean isClickAdv;

    //时间是否结束
    private boolean isTimeOver;


    @Nullable
    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_splash, null, false));
    }

    @Override
    protected boolean isTitleBarShow() {
        return false;
    }

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }


    @Override
    public void initView() {
        LogUtils.LogD("进来了", "-SplashAty-------");
        mImgBg = (ImageView) findViewById(R.id.id_start_page_bg_img);
        mImgBg.setImageResource(R.drawable.em_splash);
        if (!TextUtils.equals(StringUtils.isEmpty(Session.getAppVersion()) ? "" : Session.getAppVersion(), SystemUtils.getVersionName(this))) {
            ClearCacheUtil.clearCache(aty, 0, "");
            Session.clearMust();
            Session.setAppVersion(SystemUtils.getVersionName(this));
        }

        Session.setUserFirstInstall(false);
        // 判断用户是否是第一次安装
        if (!Session.getUserFirstInstall()) {
            initV();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
//                    skipActivity(aty, GuideAty.class);
                }
            }, 2000);
        }
    }

    @Override
    public void initData() {
        SystemUtils.checkSystem(this);
    }

    private void initV() {
        LogUtils.LogD("进来了", "-SplashAty-------initV");
        recLen = 1000 * 3;
        mImgBg.setImageResource(R.drawable.em_splash);
        mTxtjumpTo = (TextView) findViewById(R.id.id_jump_txt);
        mTxtjumpTo.setVisibility(View.VISIBLE);
        mTxtjumpTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTimeOver = true;
                jumpTo();
            }
        });
        //界面广告停留时间
        mc = new MyCount(recLen, 1000);
        mc.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isClickAdv = false;
        if (isTimeOver) {
        }
    }

    /**
     * 跳转
     */
    private void jumpTo() {
        if (!isClickAdv) {
            skipActivity(aty, MainAty.class);
        }
    }

    /**
     * 倒计时类
     */
    public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            mTxtjumpTo.setText("跳过(" + l / 1000 + "秒)");
        }

        @Override
        public void onFinish() {
            isTimeOver = true;
            jumpTo();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mc) {
            mc.cancel();
        }
    }
}
